//
//  XGAccountSettingViewController.swift
//  TodayNews
//
//  Created by monkey on 2020/2/9.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import SCLAlertView
import MBProgressHUD
import LeanCloud

class XGAccountSettingViewController: XGBaseSettingViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        navigationItem.title = "账号安全"
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        tableView.deselectRow(at: indexPath, animated: true)
        
        switch indexPath.row {
        case 0:
            changePassword()
        case 1:
            // 退出登录
            quitLogin()
        default:
            break
        }
    }
    
    /// 修改密码
    private func changePassword() -> Void
    {
        if XGUserAccountManager.shared.isClientAccount {
            // 客户端账号
            let viewController = XGChangePasswordViewController()
            navigationController?.pushViewController(viewController, animated: true)
        } else {
            let alert = SCLAlertView()
            alert.showError("提示", subTitle: "当前你的账号不支持修改密码~")
        }
    }
    
    /// 退出登录
    private func quitLogin() -> Void
    {
        let alertView = SCLAlertView()
        alertView.addButton("确定") {
            MBProgressHUD.showProgressHUD(text: "正在退出登录...")
            if XGUserAccountManager.shared.isClientAccount {
                XGUserModel.logOut()
            }
            XGUserAccountManager.shared.removeUserAccount()
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
                MBProgressHUD.hideHUD()
                NotificationCenter.default.post(name: kSwitchKeyWindowRootViewControllerNotification, object: XGLoginViewController.self, userInfo: nil)
            }
        }
        
        alertView.showInfo("提示", subTitle: "确定要退出当前账号吗?")
    }
}
